package thread;
//Callable
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo11 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //此处不需要引入额外的成员变量
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int result = 0;
                for (int i = 1; i <=1000; i++) {
                    result+=i;
                }
                return result;
            }
        };
        //FutureTask是Thread和Callable的粘合剂
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        //Thread并没有提供构造函数来传入callable
        //Thread t = new Thread(callable);×
        Thread t = new Thread(futureTask);
        t.start();
        //此处代码不需要Join,使用futureTask.get()获取结果[这个操作带有阻塞功能,如果线程还没执行完毕,线程就会阻塞]
        System.out.println(futureTask.get());
    }
}
